package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/5 15:40
 */
public class AgricultureGroomQueryFragment extends Fragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.AgricultureGroomQueryFragment.EXTRA_CONTENT";

  private final int BEGINREG_DATE = 1;
  private final int ENDREG_DATE = 2;
  @BindView(R.id.tv_beginregDate) TextView tvBeginregDate;
  @BindView(R.id.ib_beginregDate) ImageView ibBeginregDate;
  @BindView(R.id.tv_endregDate) TextView tvEndregDate;
  @BindView(R.id.ib_endregDate) ImageView ibEndregDate;
  @BindView(R.id.et_groomType) EditText etGroomType;

  private String beginDate;
  private String endDate;
  private String groomplatform;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    if (activity != null) {
      ActionBar actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(R.string.query);
      }
    }
    ExtraValueContent queryConten =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      beginDate = queryConten.getExtName1();
      endDate = queryConten.getExtName2();
      groomplatform = queryConten.getExtName3();
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(tvBeginregDate.getText().toString());
        queryConten.setExtName2(tvEndregDate.getText().toString());
        queryConten.setExtName3(etGroomType.getText().toString());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_agriculture_groom_query, container, false);
    ButterKnife.bind(this, view);
    initData();
    return view;
  }

  private void initData() {
    tvBeginregDate.setText(beginDate);
    tvEndregDate.setText(endDate);
    etGroomType.setText(groomplatform);
  }

  @OnClick({ R.id.ib_beginregDate, R.id.ib_endregDate }) public void onClick(View view) {
    switch (view.getId()) {

      case R.id.ib_beginregDate:
        showDialog(BEGINREG_DATE);
        break;
      case R.id.ib_endregDate:
        showDialog(ENDREG_DATE);
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;
    switch (requestCode) {

      case BEGINREG_DATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case ENDREG_DATE:
        dialog = new BaseYMDTimeDialogFragment();
        break;
    }
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(AgricultureGroomQueryFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginregDate.setText(beginDate);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndregDate.setText(endDate);
    }
  }

  public static AgricultureGroomQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    AgricultureGroomQueryFragment fragment = new AgricultureGroomQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}

